// 创建图片上传组件
<template>
  <div><el-upload
    class="avatar-uploader"
    action="#"
    :show-file-list="false"
    :http-request="upload"
  >
    <img v-if="imageUrl" :src="imageUrl" class="avatar">
    <i v-else class="el-icon-plus avatar-uploader-icon" />
    <el-progress v-if="showProgress" type="circle" :percentage="percentage" class="progress" />
  </el-upload>

  </div>
</template>

<script>
var COS = require('cos-js-sdk-v5')
const cos = new COS({
  SecretId: 'AKID98yYsvhBPM2SbT4OREk13qMbZGhrkLgl',
  SecretKey: 'pOhkW2HFxNWQ4RXNwEFzSespVv5yavzn'
})
export default {
  name: 'UploadPhoto',
  data() {
    return {
      imageUrl: '',
      showProgress: false, // 进度条
      percentage: 0
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    // 自定义上传属性
    upload(files) {
      // 显示进度条
      this.showProgress = true
      if (files.file) {
        cos.putObject({
          Bucket: 'ze-1308159467', /* 必须 */
          Region: 'ap-beijing', /* 存储桶所在地域，必须字段 */
          Key: files.file.name, /* 必须 */
          StorageClass: 'STANDARD',
          Body: files.file, // 上传文件对象
          //   上传的进度
          onProgress: (progressData) => {
            this.percentage = progressData.percent * 100
            // console.log(JSON.stringify(progressData))
          }
        }, (err, data) => {
          console.log(err || data)
          if (err === null) {
            this.imageUrl = `http://${data.Location}`
          }
          this.showProgress = false
        })
      }
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .progress {
  position: absolute;
  display: flex;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #e4e4e4;
  border-radius: 50%;
  opacity: 0.9;
}
</style>
